<template>
	<view>
		<view class="goods-list">
			<!--  v-for="(item,index) in goodsList" :key="index" -->
			<view v-for="(goods,index) in goodsList" :key="index" @click="goGoodsDetail(goods)">
				<goods-item @click="getGoods" :goods="goods"></goods-item>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		goodslist
	} from "@/api/goodsList.js"
	export default {
		data() {
			return {
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,//页数
					pagesize: 10,//页容量
				},
				goodsList: [],
				total:0,//总数
				isLoading:false,//节流阀
			}
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList();
		},
		methods: {
			getGoods(){
				console.log('111')
			},
			async getGoodsList() {
				// 打开节流阀
				this.isLoading = true 
				const {
					data: res
				} = await goodslist(this.queryObj);
				console.log(res)
				// 关闭节流阀
				this.isLoading = false;
				if(res.meta.status !== 200) return uni.$showMag();
				this.goodsList =[...this.goodsList,...res.message.goods];
				this.total = res.message.total;
				console.log(this.total)
				console.log(this.goodsList)
			},
			goGoodsDetail(goods){
				console.log(goods.goods_id)
				uni.navigateTo({
					url:'/pages-supper/swiper/swiper?goods_id='+ goods.goods_id
				})
			}
		},
		onReachBottom(){
			console.log(111);
			
			// 提示数据加载完毕
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMag();
			// 判断节流阀是否是关闭还是开启,
			// 节流阀为true时，在此次截取return  禁止发送请求   为false时可以发送请求
			if(this.isLoading) return;
			// 每次上拉触发时，页数加一
			this.queryObj.pagenum += 1;
			
			this.getGoodsList()
		},
		onPullDownRefresh(){
			console.log(222);
			// 下拉刷新时，数据刷新
			this.queryObj.pagenum = 1;
			this.total = 0;
			this.isLoading = false;
			this.goodsList = [];
			// 重新发送请求
			this.getGoodsList(()=>uni.stopPullDownRefresh());
		}
		
		
	}
</script>

<style lang="scss">
	.goods-list {
		
	}
</style>
